<template>


	<view>
		<!-- 登录页面正中间图标 -->
		<view class="logo">
			<view class="img">
				<image mode="widthFix"
					src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Fd0%2Fa5%2F86%2Fd0a586e0cece1f1dec648e9aa3d8c9d6.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633761077&t=84f5ce7dc4918ef2e89d1fa4e1e72504">
				</image>
			</view>
		</view>
		<!-- 注册界面 -->
		<view class="form userform">
			<view class="username">
				<view class="get-code" :style="{'color':getCodeBtnColor}" @click.stop="getCode()">{{getCodeText}}</view>
				<input placeholder="请输入手机号" v-model="phoneNumber" placeholder-style="color: rgba(255,255,255,0.8);" />
			</view>
			<view class="code">
				<input placeholder="请输入验证码" v-model="code" placeholder-style="color: rgba(255,255,255,0.8);" />
			</view>
			<view class="btn" @tap="toLogin">登录</view>
			<view class="res">
				<view @tap="doReg">没有账号立即注册</view>
			</view>
		</view>

	</view>
</template>

// <script>
	import {
		request
	} from '@/network/http.js'
	export default {
		data() {
			return {
				phoneNumber: "",
				code: '',
				getCodeText: '获取验证码',
				getCodeBtnColor: "#ffffff",
				getCodeisWaiting: false
			}
		},
		onLoad(option) {
			if(option!=null){this.phoneNumber=option.phoneNumber}
			
		},
		methods: {
			doReg(){
				uni.navigateTo({
					url:'/pages/zhuce/zhuce'
				})
			},
			Timer() {},
			getCode() {
				uni.hideKeyboard() //隐藏已经显示的软键盘，如果软键盘没有显示则不做任何操作。
				if (this.getCodeisWaiting) {
					return;
				}
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phoneNumber))) { //校验手机号码是否有误
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					return false;
				}
				this.getCodeText = "发送中..." //发送验证码
				request({
					url: 'driver/ssmsj/sendcode',
					method: 'POST',
					data: {
						phone: this.phoneNumber
					},
				})
				this.getCodeisWaiting = true;
				this.getCodeBtnColor = "rgba(255,255,255,0.5)" //追加样式，修改颜色
				//示例用定时器模拟请求效果
				//setTimeout(()用于在指定的毫秒数后调用函数或计算表达式
				setTimeout(() => {
					uni.showToast({
						title: '验证码已发送',
						icon: "none"
					}); //弹出提示框
					//示例默认1234，生产中请删除这一句。
					// this.code = '1234'; //发送验证码，进行填入
					this.setTimer(); //调用定时器方法
				}, 1000)
			},
			//setTimer： 需要每隔一段时间执行一件事的的时候就需要使用SetTimer函数
			setTimer() {
				let holdTime = 60; //定义变量并赋值
				this.getCodeText = "重新获取(60)"
				//setInterval（）是一个实现定时调用的函数，可按照指定的周期（以毫秒计）来调用函数或计算表达式。
				//setInterval方法会不停地调用函数，直到 clearInterval被调用或窗口被关闭。
				this.Timer = setInterval(() => {
					if (holdTime <= 0) {
						this.getCodeisWaiting = false;
						this.getCodeBtnColor = "#ffffff";
						this.getCodeText = "获取验证码"
						clearInterval(this.Timer); //清除该函数
						return; //返回前面
					}
					this.getCodeText = "重新获取(" + holdTime + ")"
					holdTime--;
				}, 1000)
			},
			// 	uni.showLoading({
			// 		title: '提交中...'
			// 	})
			// 	//模板示例把用户注册信息储存在本地，实际使用中请替换为上传服务器。
			// 	setTimeout(() => {
			// 		uni.getStorage({
			// 			key: 'UserList',
			// 			success: (res) => {
			// 				//增加记录，密码md5
			// 				res.data.push({
			// 					username: this.phoneNumber
			// 				})
			// 				uni.setStorage({
			// 					key: 'UserList',
			// 					data: res.data,
			// 					success: function() {
			// 						uni.hideLoading()
			// 						uni.showToast({
			// 							title: '注册成功',
			// 							icon: "success"
			// 						});
			// 						setTimeout(function() {
			// 							uni.navigateBack();
			// 						}, 1000)
			// 					}
			// 				});
			// 			},
			// 			fail: (e) => {
			// 				uni.hideLoading()
			// 				console.log('error');
			// 				//新建UserList
			// 				uni.setStorage({
			// 					key: 'UserList',
			// 					data: [{
			// 						username: this.phoneNumber
			// 					}],
			// 					success: function() {
			// 						uni.hideLoading()
			// 						uni.showToast({
			// 							title: '注册成功',
			// 							icon: "success"
			// 						});
			// 						setTimeout(function() {
			// 							uni.navigateBack();
			// 						}, 1000)
			// 					},
			// 					fail: function(e) {
			// 						console.log('set error:' + JSON.stringify(e));
			// 					}
			// 				});
			// 			}
			// 		});
			// 	}, 1000)
			// },
			toLogin() {
				uni.hideKeyboard() //隐藏已经显示的软键盘，如果软键盘没有显示则不做任何操作。
				request({
					url: 'driver/ssmsj/login',
					method: 'POST',
					data: {
						phone: this.phoneNumber,
						code: this.code
					},
				}).then(res => {
					console.log(123123)
					if (res.data.code == 0) {
						console.log(res),
							uni.setStorageSync('token', res.data.token),
							uni.setStorageSync('refreshToken', res.data.refreshToken),
							uni.showToast({
								title: '登录成功',
								icon: 'none',
								duration: 1000,
								success: function() {
									setTimeout(function() {
										uni.switchTab({
											url: "../gerenxx/gerenxx"
										})
									}, 1000)
								}
							})
					} else {
						console.log(res.data.msg)
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
				// uni.redirectTo({
				// 	url: 'login'
				// }); //跳转到登录页
				// uni.navigateBack(); //关闭当前页面，返回上一页面
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #dcdcdc; //整一个页面的背景颜色
		// linear-gradient(to bottom, #f06c7a 0%, #f06c7a 100%);
		height: 100%;
	}

	.icon {
		color: #ffaa00;
	}

	.logo {
		width: 100%;
		height: 45vw;
		display: flex;
		justify-content: center;
		align-items: center;

		.img {
			width: 25%;
			height: 25vw;

			image {
				width: 100%;
				border-radius: 100%;
			}
		}
	}

	.form {
		width: 86%;
		padding: 0 7%;
		font-size: 30upx;

		.username,
		.password,
		.code {
			width: calc(100% - 90upx);
			height: 90upx;
			display: flex;
			align-items: center;
			border-radius: 45upx;
			background-color: rgba($color: #ffffff, $alpha: 0.1);
			padding: 0 45upx;
			margin-bottom: 26upx;

			input {
				width: 100%;
				height: 50upx;
				color: rgba($color: #000000, $alpha: 0.8);
				font-weight: 200;
			}
		}

		.btn {
			color: #f06c7a;
			width: 100%;
			height: 90upx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 45upx;
			background-color: #ffd117;
			font-size: 40upx;
		}
	}

	.userform {
		.username {
			position: relative;

			.get-code {
				position: absolute;
				height: 90upx;
				display: flex;
				align-items: center;
				justify-content: center;
				right: 0;
				padding: 0 40upx;
				z-index: 3;

				&:after {
					//点击以后，左边出现白色的线
					content: " ";
					width: 1upx; //宽度为1upx
					height: 50upx; //高度为50upx
					background-color: #ffd21c; //背景颜色为白色
					position: absolute;
					z-index: 3;
					margin-right: 100%;
					left: 0;
					top: 20upx;
				}
			}
		}

		.res {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100upx;
			color: rgba($color: #f06c7a, $alpha: 0.8);
		}
	}
</style>
